package com.hohai.mall.common.framework.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @author: Zhao Rongjie
 * @create: 2019-07-02 14:21
 **/
public class NewDateUtil {

    private static final ZoneId ZONE = ZoneId.systemDefault();

    /**
     * Format:yyyy-MM-dd
     */
    public static final String FORMATE_STRING_1 = "yyyy-MM-dd";

    /**
     * Format:yyyyMMdd
     */
    public static final String FORMATE_STRING_2 = "yyyyMMdd";

    /**
     * Format:yyyy-MM-dd HH:mm:ss
     */
    public static final String FORMATE_STRING_3 = "yyyy-MM-dd HH:mm:ss";


    /**
     * 计算两个日期的间隔天数
     *
     * @param startDate 开始时间，如：2008-12-03 11:00:00
     * @param endDate   结束时间，如：2009-12-31 11:00:00
     * @return long 间隔天数(long)；startDate > endDate,返回天数为正数，否则返回为对应天数负数
     */
    public static long getBetweenDays(Date startDate, Date endDate) {
        if (endDate == null || startDate == null) {
            return -1;
        }
        LocalDateTime startDateTime = convertToLocalDateTime(startDate);
        LocalDateTime endDateTime = convertToLocalDateTime(endDate);
        Duration duration = Duration.between(startDateTime, endDateTime);
        return duration.toDays();
    }


    private static LocalDateTime convertToLocalDateTime(Date startDate) {
        Instant instant = startDate.toInstant();
        return LocalDateTime.ofInstant(instant, ZONE);
    }


    /**
     * 获取指定月第一天00:00:00
     *
     * @param date
     * @return
     */
    public static Date getFirstThisDate(Date date) {
        LocalDateTime localDateTime = convertToLocalDateTime(date);
        LocalDateTime firstDayOfMonth = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
        return Date.from(firstDayOfMonth.atZone(ZONE).toInstant());
    }


    /**
     * 获取指定时间的月份最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastThisDate(Date date) {
        LocalDateTime localDateTime = convertToLocalDateTime(date);
        LocalDateTime firstDayOfMonth = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MIN);
        return Date.from(firstDayOfMonth.atZone(ZONE).toInstant());
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static long getCurrentTimeLong() {
        return System.currentTimeMillis();
    }

    /**
     * 日期格式化
     *
     * @param date
     * @param pattern
     * @return
     */
    public static Date formateDate(Date date, String pattern) {
        String dateStr = "";
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        try {
            if (date != null) {
                dateStr = formatter.format(date);
            }
            return formatter.parse(dateStr);
        } catch (ParseException e) {
        }
        return null;
    }


}
